using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Threading;
using System.Diagnostics;

namespace DiskGarden
{
	public partial class FrmSearch : Form
	{

		private CategoryNode root;
		public CategoryNode Root
		{
			get { return root; }
			set { root = value; }
		}

		public FileNode node;
		private SearchEngine searchEngine = null;

		DateTime dtStart = DateTime.Now;


		public FrmSearch(CategoryNode root)
		{
			InitializeComponent();
			Localize();
			this.root = root;
			listView.SmallImageList = Icons.ImgList;
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			searchEngine = new SearchEngine(root);
			searchEngine.NodeFound += new NodeEventHandler(Engine_NodeFound);
			searchEngine.SearchCompleted += new BasicEventHandler(Engine_SearchCompleted);
		}

		private void Localize()
		{
			Locale locale = Locale.GetInstance();
			this.Text = locale.Find;
			btnFind.Text = locale.Find;
			boxFindWhat.Text = locale.FindWhat;
			columnHeaderDate.Text = locale.Modified;
			columnHeaderName.Text = locale.Name;
			columnHeaderSize.Text = locale.Size;
			columnHeaderPath.Text = locale.Path;
			columnHeaderDisk.Text = locale.Disk;
		}

		private void btnFind_Click(object sender, EventArgs e)
		{
			if (searchEngine.IsSearchingNow)
				searchEngine.StopSearch();
			else
			{
				listView.Items.Clear();
				btnFind.Text = Locale.I.Stop;
				Application.DoEvents();

				dtStart = DateTime.Now;
				Trace.WriteLine(dtStart.ToLongTimeString(), "Begin searching");
				searchEngine.StartSearch(txtSearch.Text);
			}
		}

		private void Engine_SearchCompleted()
		{
			if (this.InvokeRequired)
				this.Invoke(new BasicEventHandler(Engine_SearchCompleted));
			else
			{
				AddToListFromBuffer();
				btnFind.Text = Locale.I.Find;
				Trace.WriteLine(DateTime.Now.ToLongTimeString(), "End searching");
				Trace.WriteLine((DateTime.Now - dtStart).TotalMilliseconds, "Searching time");
				Trace.WriteLine(listView.Items.Count, "Found files");
			}
		}

		private DateTime lastTimeInfoChanged = DateTime.Now;
		private List<ListViewItem> itemsBuffer = new List<ListViewItem>();

		private void Engine_NodeFound(Node node)
		{
			FileNode fileNode = node as FileNode;
			ListViewItem newItem = new ListViewItem(node.Name);
			newItem.Tag = node;
			newItem.SubItems.Add(fileNode.GetVolume().Name);
			newItem.SubItems.Add(node.Size.ToString("# ### ### ###"));
			newItem.SubItems.Add(node.Path);
			newItem.SubItems.Add(node.Date.ToString());
			newItem.ImageKey = node.GetImageKey();
			itemsBuffer.Add(newItem);

			if ((DateTime.Now - lastTimeInfoChanged).TotalMilliseconds > 30)
			{
				AddToListFromBuffer();
				lastTimeInfoChanged = DateTime.Now;
			}
		}

		private void AddToListFromBuffer()
		{
			if (listView.InvokeRequired)
			{
				listView.Invoke(new BasicEventHandler(AddToListFromBuffer));
			}
			else
			{
				listView.Items.AddRange(itemsBuffer.ToArray());
			}
			if (itemsBuffer.Count > 0)
				itemsBuffer.Clear();
		}



		private void ListViewItemActivate(object sender, EventArgs e)
		{
			node = (FileNode)listView.FocusedItem.Tag;
			this.DialogResult = DialogResult.OK;
		}

		private void txtSearch_KeyUp(object sender, KeyEventArgs e)
		{
			if (e.KeyData == Keys.Enter) btnFind_Click(null, null);
		}

	}
}
